<?php

define('ACC', true);
require ('../includes/init.php');
$cat = new CatModel();

if($_GET['act'] == 'list'){
	
	$list = $cat->getList();
	$category_list = $cat->catSort($list);

	function lev($args){
		$str = '';
		for($i=0,$len=$args['times']; $i<$len; $i++){
			$str .= '&nbsp;';
		}
		return $str;
	}

	$smarty->assign('category_list', $category_list);
	$smarty->assign('title', '商品分类');
	$smarty->assign('url', 'category.php?act=add');
	$smarty->assign('link', '添加分类');
	$smarty->assign('action', 'add');
	$smarty->register_function('lev', 'lev');
	$smarty->display('./admin/category_list.html');
	
}else if($_GET['act'] == 'add'){
	
	$list = $cat->getList();
	$category_list = $cat->catSort($list);
	
	foreach($category_list as $key => $val){
		$str = '';
		for($i=0,$len=$val['lev']; $i<$len; $i++){
			$str .= '&nbsp;';
		}
		$category_list[$key]['lev'] = $str;
	}
	
	$cat_list = $cat->get_cat_list();
	
	$smarty->assign('cat_list', $cat_list);
	$smarty->assign('category_list', $category_list);
	$smarty->assign('title', '添加分类');
	$smarty->assign('url', 'category.php?act=list');
	$smarty->assign('link', '商品分类');
	$smarty->assign('action', 'insert');
	$smarty->assign('judge', 'add');
	
	$smarty->display('./admin/category_info.html');
	
}else if($_GET['act'] == 'insert'){
	
	$data = array();
	if(!isset($_POST['cat_name']) || trim($_POST['cat_name']) == ''){
		echo '分类名不能为空！';
		exit;
	}
	$data['cat_name'] = trim($_POST['cat_name']);
	
	if(!isset($_POST['parent_id']) || intval($_POST['parent_id']) <= 0){
		echo '分类名不能为空！';
		exit;
	}
	$data['parent_id']=intval($_POST['parent_id']);
	
	if(!isset($data['cat_name'])){
		
	}
	if($data['parent_id']<=0){
		echo '参数有误！';
		exit;
	}
	
	if($cat->add($data)){
		$desc = '添加' . $data['cat_name'] . '成功';
		$url = 'category.php?act=list';
		$link = '商品分类';
		admin_msg($smarty, $desc, $url, $link);
	}else{
		$smarty->assign('message', '您没有权限进行此操作！');
		$smarty->assign('title', '系统提示');
		$smarty->assign('url', 'javascript:history.go(-1);');
		$smarty->assign('link', '返回');
		$smarty->display('./admin/error.html');
	}
	
}else if($_GET['act'] == 'edit'){
	
	$cat_id = intval($_GET['cat_id']);
	$cat_info = $cat->getRow($cat_id);
	
	$smarty->assign('cat', $cat_info);
	$smarty->assign('title', '编辑商品分类');
	$smarty->assign('url', 'category.php?act=add');
	$smarty->assign('link', '商品分类');
	$smarty->assign('action', 'update');
	$smarty->assign('judge', 'edit');
	
	$smarty->display('./admin/category_info.html');
	
}else if($_GET['act'] == 'update'){
	
	$cat_id=$_POST['cat_id']+0;
	$data['cat_name'] = trim($_POST['cat_name']);
	$data['is_show'] = trim($_POST['is_show']);
	
	if($cat->up($data,$cat_id)){
		$desc = '修改' . $data['cat_name'] . '成功';
		$url = 'category.php?act=list';
		$link = '商品类型列表';
		admin_msg($smarty, $desc, $url, $link);
	}else{
		echo '修改失败！';
	}
	
}else if($_GET['act'] == 'delete'){
	
	$cat_id=$_GET['id']+0;
	if($cat_id<=0){
		echo 0;
		exit;
	}
	
	$son=$cat->getSon($cat_id);
	if(!empty($son)){
		echo 3;
		exit;
	}

	if($cat->del($cat_id)){
		echo 1;
		exit;
	}else{
		echo 0;
		exit;
	}
	
}else if($_GET['act'] == 'move'){
	
	$cat_id = $_GET['cat_id'] + 0;
	if(!$cat_id){
		echo '数据有误!';
		return;
	}
	
	$row = $cat->getRow($cat_id);
	
	$arr = $cat->getListInfo();
	$cat_list = $cat->catSort($arr);
	for($i=0,$len=count($cat_list); $i<$len; $i++){
		$str = '';
		for($j=0,$len=$cat_list[$i]['lev']; $j<$len; $j++){
			$str .= '&nbsp;';
		}
		$cat_list[$i]['lev'] = $str;
	}

	$smarty->assign('cat_list', $cat_list);
	$smarty->assign('row', $row);
	$smarty->assign('title', '转移商品');
	$smarty->assign('url', 'category.php?act=list');
	$smarty->assign('link', '商品分类');
	$smarty->assign('action', 'move_ok');
	$smarty->assign('judge', 'move');
	
	$smarty->display('./admin/category_info.html');
	
}else if($_GET['act'] == 'move_ok'){

	$cat_id = $_POST['cat_id'] + 0;
	$target_cat_id = $_POST['target_cat_id'] + 0;
	
	if($cat_id <= 0 || $target_cat_id <= 0){
		echo '参数非法！';
		exit;
	}
	if($cat_id < 50){
		echo '不能转移顶级栏目！';
		exit;
	}
	
	$list=$cat->getList();
	$list=$cat->catSort($list,$cat_id);
	
	$arr=array();
	$arr[]=$cat_id;
	foreach($list as $v){
		$arr[]=$v['cat_id'];
	}
	
	if(in_array($target_cat_id, $arr)){
		echo '不能用自身或子孙栏目做父栏目！';
		exit;
	}
	
	if($cat->move($cat_id, $target_cat_id)){
		$desc = '转移'.'name'.'成功';
		$url = 'category.php?act=list';
		$link = '商品分类';
		admin_msg($smarty, $desc, $url, $link);
	}else{
		echo '转移失败!';
		exit;
	}
	
}else if($_GET['act'] == 'search'){
	
	$cat_id = $_GET['cat_id'];

	$cat_attrs = $cat->get_cat_attrs($cat_id);

	echo json_encode($cat_attrs);
	return;
	
}



?>